# 股票模型
from datetime import datetime

# 从app中导入共享的db实例
from app import db

class Stock(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    code = db.Column(db.String(20), unique=True, nullable=False)
    name = db.Column(db.String(100), nullable=False)
    market = db.Column(db.String(50))
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 关系
    portfolio_stocks = db.relationship('PortfolioStock', backref='stock', lazy=True)
    transactions = db.relationship('Transaction', backref='stock', lazy=True)
    
    def __repr__(self):
        return f'<Stock {self.code}: {self.name}>'